package com.yxq.mdt.app.modules.shoppingcart.ui;

import android.widget.CheckBox;
import android.widget.CompoundButton;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.application.IntentHelper;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.goodsdetail.ui.GoodsCommentImagesListAdapter;
import com.yxq.mdt.app.modules.shoppingcart.bean.CartBean;
import com.yxq.mdt.app.utils.GridSpacingItemDecoration;
import com.yxq.mdt.app.widgets.CircleImageView;

import java.util.List;

/**
 * @author L
 * @time 2019-10-21
 * @describe 购物车列表
 */
public class CartListAdapter extends BaseQuickAdapter<CartBean.ListBean, BaseViewHolder> {

    private boolean showCheckBox = true;

    public void setShowCheckBox(boolean showCheckBox) {
        this.showCheckBox = showCheckBox;
    }

    public boolean isShowCheckBox() {
        return showCheckBox;
    }

    public interface CartCheckedInterface {
        void checked(int position);
    }

    private CartCheckedInterface cartCheckedInterface;

    public void setCartCheckedInterface(CartCheckedInterface cartCheckedInterface) {
        this.cartCheckedInterface = cartCheckedInterface;
    }

    public CartListAdapter(int layoutResId, @Nullable List<CartBean.ListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, CartBean.ListBean item) {

        helper.setText(R.id.cart_tv_name, item.getGoods_name() + "");
        GlideHelper.setLoadRoundImage(mContext, item.getGoods_pic(), R.color.gray, helper.getView(R.id.cart_iv_img));

        helper.setText(R.id.cart_tv_attr, (getAttr(item.getAttr_list())));
        helper.setText(R.id.cart_tv_price, String.format("￥%.2f", item.getUnitPrice()));
        //做最大库存数目判断
        if (item.getNum() > item.getMax_num()) {
            item.setNum(item.getMax_num());
        }
        helper.setText(R.id.tv_num, item.getNum() + "");
        //判断无货
        helper.setVisible(R.id.cart_tv_alert, item.getMax_num() == 0);

        if (isShowCheckBox()) {

            helper.addOnClickListener(R.id.tv_reduce);
            helper.addOnClickListener(R.id.tv_add);

            CheckBox checkBox = helper.getView(R.id.cart_check_box);
            checkBox.setOnCheckedChangeListener(null);    //先把点击事件取消
            checkBox.setChecked(item.isChecked());
            checkBox.setOnCheckedChangeListener((compoundButton, b) -> {
                item.setChecked(b);
                if (cartCheckedInterface != null) {
                    cartCheckedInterface.checked(getParentPosition(item));
                }
            });

        }
    }

    private String getAttr(List<CartBean.ListBean.AttrListBean> attrListBeanList) {

        if (attrListBeanList != null && attrListBeanList.size() > 0) {
            StringBuffer sb = new StringBuffer();
            for (CartBean.ListBean.AttrListBean attrListBean : attrListBeanList) {
                sb.append(attrListBean.getAttr_name());
                sb.append(" ");
            }
            return sb.toString();

        }
        return "默认";
    }
}
